{% load static %}
<!doctype html>
<html lang="en">
    <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

        <!-- Bootstrap CSS -->
        <link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
        <link href="{% static 'login/css/register.css' %}" rel="stylesheet" />

        <!-- Optional JavaScript -->
        <!-- jQuery first, then Popper.js, then Bootstrap JS -->

        <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://cdn.bootcss.com/popper.js/1.15.0/umd/popper.min.js"></script>
        <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>

        <title>注册</title>
    </head>
    <body>

        <div class="container">
            <div class="col">
                <form class="form-register" action="/register/" method="post">

                    {% if register_form.captcha.errors %}
                    <div class="alert alert-warning">{{ register_form.captcha.errors }}</div>
                    {% elif message %}
                    <div class="alert alert-warning">{{ message }}</div>
                    {% endif %}

                    <!--对表单进行csrf验证加持，防止跨站请求伪造-->
                    {% csrf_token %}

                    <h3 class="text-center">欢迎注册</h3>

                    <div class="form-group">
                        {{ register_form.username.label_tag }}
                        {{ register_form.username}}
                    </div>
                    <div class="form-group">
                        {{ register_form.password1.label_tag }}
                        {{ register_form.password1 }}
                    </div>
                    <div class="form-group">
                        {{ register_form.password2.label_tag }}
                        {{ register_form.password2 }}
                    </div>
                    <div class="form-group">
                        {{ register_form.email.label_tag }}
                        {{ register_form.email }}
                    </div>
                    <div class="form-group">
                        {{ register_form.sex.label_tag }}
                        {{ register_form.sex }}
                    </div>
                    <div class="form-group">
                        {{ register_form.captcha.label_tag }}
                        {{ register_form.captcha }}
                    </div>


                    <div>
                        <a href="/login/" class="text-success"><ins>登录</ins></a>
                        <button type="submit" class="btn btn-primary float-right">注册</button>
                    </div>
                </form>
            </div>
        </div> <!-- /container -->
        {# 刷新验证码的脚本，放到body部分的最后面即可 #}
        {# 其中，{% url 'captcha_refresh' %}的路由是captcha自己的路由: #}
        <script>
            $('.captcha').click(function() {
                $.getJSON('{% url 'captcha_refresh' %}', function(result) {
                    $('.captcha').attr('src', result['image_url']);
                    $('#id_captcha_0').val(result['key']);
                });
            });
        </script>
    </body>
</html>
